#include "PLT_HEADS.h"
#include "KX_TempChange.h"

/*-------------------------------------------------------------------------------------------------------------
Note: 建立探头对应的温度表格后调用Fun_GetTemp即可获得转换后的温度。需要定义n_NodeNumSum节点数
-----------------------------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------------------------*/
// 静态变量用于温度转换模块
unsigned int U16_TempValue = 0;       // 转换后温度值
unsigned int U16_TempAdValue = 0;     // 采样的AD值
/*---------------------------------------------------------------------------------------------------------*/
// 温度表格 示例
code const TEMP_STRUCT TemperatureTable[] = 
{
    {0   ,2032},{20  ,1956},{40  ,1887},{60  ,1822},{80  ,1762},
    {100 ,1706},{120 ,1654},{140 ,1606},{160 ,1560},{180 ,1517},
    {200 ,1477},{220 ,1439},{240 ,1403},{260 ,1369},{280 ,1337},
    {300 ,1307},{320 ,1278},{340 ,1250},{360 ,1224},{380 ,1199},
    {400 ,1175},{420 ,1153},{440 ,1131},{460 ,1110},{480 ,1090},
    {500 ,1071},{520 ,1053},{540 ,1036},{560 ,1019},{580 ,1003}
};
/*===========================================================================================================
Function    :ADC_TO_TEMPERATURE
Input       :adcResult - ADC采样值
             tab - 温度对照表
             tabsize - 温度对照表长度
Output      :转换后的温度值
Description :将探头AD值转换成实际温度
===========================================================================================================*/
unsigned int ADC_TO_TEMPERATURE(unsigned int adcResult, TEMP_STRUCT *tab,unsigned char tabsize)
{
    unsigned char i;
    unsigned int U16_TempResult = 0;
    unsigned char U8_TempNodeFlag = 0;
    
    if (tab == ((void*)0) || adcResult == 0)
    {
        // 错误处理
        return 0;
    }       
    // 检查表格有效性
    if (tabsize <= 0)
    {
        return 0;
    }
    
    // 处理超出范围的ADC值
    if (adcResult < tab[tabsize - 1].U16_TempNodeAdcValue)
    {
        return tab[tabsize - 1].U16_TempNode;
    }
    else if (adcResult > tab[0].U16_TempNodeAdcValue)
    {
        return 0;
    }
    
    // 查找ADC值在温度表中的区间
    for(i = 0; i < tabsize; i++)
    {
        if (adcResult == tab[i].U16_TempNodeAdcValue)
        {
            U8_TempNodeFlag = 1;
            U16_TempResult = tab[i].U16_TempNode;
            break;
        }
        else if (adcResult > tab[i].U16_TempNodeAdcValue)
        {
            break;
        }
    }
    
    // 如果没有找到精确匹配，使用线性插值
    if (!U8_TempNodeFlag)
    {
        // 使用直线方程两点式求解，温度为横坐标，AD值为纵坐标
        // 为避免整数溢出，使用分步计算
        unsigned int deltaTemp;
        unsigned int deltaADC;
        unsigned int adcDiff;
        deltaTemp = tab[i].U16_TempNode - tab[i - 1].U16_TempNode;
        deltaADC = tab[i - 1].U16_TempNodeAdcValue - tab[i].U16_TempNodeAdcValue;
        adcDiff = tab[i - 1].U16_TempNodeAdcValue - adcResult;
        
        // 防止除以零
        if (deltaADC == 0)
        {
            return 0;
        }
        
        // 分步计算温度值
        U16_TempResult = deltaTemp * adcDiff;
        U16_TempResult = U16_TempResult * TEMPERATURE_SCALE_FACTOR;
        U16_TempResult = U16_TempResult / deltaADC;
        U16_TempResult += tab[i - 1].U16_TempNode * TEMPERATURE_SCALE_FACTOR;
        U16_TempResult += TEMPERATURE_ROUNDING_OFFSET;
        U16_TempResult = U16_TempResult / TEMPERATURE_SCALE_FACTOR;
    }
    
    return U16_TempResult;
}

/*===========================================================================================================
Function    :GET_TEMPERATURE
Input       :adcResult - AD转换结果
             tab - 温度对照表
             tabsize - 温度对照表的长度
Output      :转换后的温度值
Description :获取温度值
===========================================================================================================*/
unsigned int GET_TEMPERATURE(unsigned int adcResult, TEMP_STRUCT *tab,unsigned char tabsize)
{
    if (tab == ((void*)0))
    {
        return 0;
    }
    
    return ADC_TO_TEMPERATURE(adcResult,tab,tabsize);
}

/*===========================================================================================================
Function    :TEMPERATURE_TASK_HANDLER
Input       :None
Output      :None
Description :温度处理任务
===========================================================================================================*/
void TEMPERATURE_TASK_HANDLER(void)
{
    // 示例：使用实际ADC值更新温度
    U16_TempValue = GET_TEMPERATURE(U16_TempAdValue, TemperatureTable,TABLE_SIZE(TemperatureTable));
}